/**
 * 功能描述
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper")
	var store =require("store");
	var activeUser = store.get("user");
	var HandlerBars = require("handlebars");
	var img="assets/eling/elcms/travel/roompanorama/src/assets/img/";
	var roomTpl = require("../assets/tpl/roompanorama_room.tpl");
	var Dialog = require("dialog");
	var RoomPanoramaDialog = require("../component/roomPanoramaDialog");
	
    module.exports = {
    	setData:function(status,widget){
    		var subnav=widget.get("subnav");
    		widget.set("curStatus",status);
    		var startDate = moment().startOf("day").valueOf();
    		var endDate = moment().endOf("day").valueOf();
    		
    		if(widget.get("model").flag == "CheckIn"){
    			var form = widget.get("form");
    			startDate = form.getValue("checkInDate");
    			endDate = form.getValue("checkOutDate");
    		}else if(widget.get("model").flag == "ExchangeRoom"){
    			var form = widget.get("form");
    			startDate = form.getValue("startDate");
    			endDate = form.getValue("endDate");
    		}
    		
    		aw.ajax({
    			url:"api/room/roompanorama",
    			data:{
    				orgBuilding:subnav.getValue("building"),
					roomType:subnav.getValue("roomType"),
					start: startDate,
					end: endDate,
					status:status,
					fetchProperties:"floor,roomlist.*"
				},
				success: function(data){
					for ( var i in data) {
						var floor =data[i];
						for ( var i in floor.roomlist) {
							var room=floor.roomlist[i]
							if(!room.vipPrice){
								room.vipPrice="未定义";
							}else{
								room.vipPrice=room.vipPrice.toFixed(2);
							}
							if(!room.standardPrice){
								room.standardPrice="未定义";
							}else{
								room.standardPrice=room.standardPrice.toFixed(2);
							}
							if(!room.protocolPrice){
								room.protocolPrice="未定义";
							}else{
								room.protocolPrice=room.protocolPrice.toFixed(2);
							}
							room.status = status;
						}
					}
					widget.print(data,widget,status);
				}
    		});
    		aw.ajax({
    			url:"api/room/statistics",
    			data:{
    				orgBuilding:subnav.getValue("building"),
					roomType:subnav.getValue("roomType"),
					start: startDate,
					end: endDate,
					all:false
				},
				success: function(ret){
					var num = ret[0];
					widget.renderRoomNumber(num);
				}
    		});
    	},
    	loadRoomType:function(widget,orgBuilding){
    		aw.ajax({
    			url:"api/roomtype/query",
    			data:{
    				orgBuildings:orgBuilding,
    				fetchProperties:"pkRoomType,name"
    			},
				success: function(data){
					widget.get("subnav").setData("roomType",data);
				}
    		})
    	},
    	print:function(room,widget,status){
    		
    		widget.set("roomData",room);
    		
    		var html = HandlerBars.compile(roomTpl)(room);
        	$(".J-room",widget.element).html(html);
        	//判断调用入口
        	if(widget.get("model").flag == "RoomPanorama"){
        		$(".el-room-check",widget.element).hide();
        		$(".room-piece").removeClass("room-select");
        	}else{
        		if(status == "Clear" || status == "Dirty" || status == "Repairing"){
    				$(".el-room-check",widget.element).show();
    				$(".room-piece").addClass("room-select");
    				this.setRoomPersonInfo();
    			}else{
    				$(".el-room-check",widget.element).hide();
    				$(".room-piece").removeClass("room-select");
    			}
        	}
    	},
    	renderRoomNumber:function(data){
    		$(".el-status-clear>.roomNumber>span").text(data.clear ? data.clear : 0);
    		$(".el-status-dirty>.roomNumber>span").text(data.dirty ? data.dirty : 0);
    		$(".el-status-checkin>.roomNumber>span").text(data.checkIn ? data.checkIn : 0);
    		$(".el-status-willgo>.roomNumber>span").text(data.willGo ? data.willGo : 0);
    		$(".el-status-appoint>.roomNumber>span").text(data.appoint ? data.appoint : 0);
    		$(".el-status-repairing>.roomNumber>span").text(data.repairing ? data.repairing : 0);
    		$(".el-status-temporary>.roomNumber>span").text(data.temporary ? data.temporary : 0);
    		$(".el-status-selfuse>.roomNumber>span").text(data.selfUse ? data.selfUse : 0);
    		$(".el-status-free>.roomNumber>span").text(data.free ? data.free : 0);
    	},
    	changeImg:function(className,imgName){
    		//重置所有
    		$(".el-status-clear-img").attr("src",img+"1jingfang.png");
    		$(".el-status-dirty-img").attr("src",img+"2zangfang.png");
    		$(".el-status-checkin-img").attr("src",img+"3zaizhufang.png");
    		$(".el-status-willgo-img").attr("src",img+"4jiangzoufang.png");
    		$(".el-status-appoint-img").attr("src",img+"5yuding.png");
    		$(".el-status-repairing-img").attr("src",img+"6weixiufang.png");
    		$(".el-status-temporary-img").attr("src",img+"7linshitai.png");
    		$(".el-status-selfuse-img").attr("src",img+"8ziyongfang.png");
    		$(".el-status-free-img").attr("src",img+"9mianfeifang.png");
    		$(".roomNumber").removeClass("white");
    		//设置图片
    		$(".el-status-"+className+"-img").attr("src",img+imgName);
    		$(".el-status-"+className+">.roomNumber").addClass("white");
    	},
    	events:{
    		"click .el-status-clear":function(e){
    			this.changeImg("clear","1jingfang_chose.png");
    			this.setData("Clear",this);
    		},
    		"click .el-status-dirty":function(e){
    			this.changeImg("dirty","2zangfang_chose.png");
    			this.setData("Dirty",this);
    		},
    		"click .el-status-checkin":function(e){
    			this.changeImg("checkin","3zaizhufang_chose.png");
    			this.setData("CheckIn",this);
    		},
    		"click .el-status-willgo":function(e){
    			this.changeImg("willgo","4jiangzoufang_chose.png");	
    			this.setData("WillGo",this);
		    },
		    "click .el-status-appoint":function(e){
		    	this.changeImg("appoint","5yuding_chose.png");	
		    	this.setData("Appoint",this);
		    },
		    "click .el-status-repairing":function(e){
		    	this.changeImg("repairing","6weixiufang_chose.png");	
		    	this.setData("Repairing",this);
		    },
		    "click .el-status-temporary":function(e){
		    	this.changeImg("temporary","7linshitai_chose.png");	
		    	this.setData("Temporary",this);
		    },
		    "click .el-status-selfuse":function(e){
		    	this.changeImg("selfuse","8ziyongfang_chose.png");	
		    	this.setData("SelfUse",this);
		    },
		    "click .el-status-free":function(e){
		    	this.changeImg("free","9mianfeifang_chose.png");
		    	this.setData("Free",this);
		    },
		    "click .room-select":function(e){
		    	var that = this;
		    	if($(e.currentTarget).attr("data-occupyDate")){
		    		Dialog.alert({
		    			content:"该房间已预约！"
		    		});
		    		return;
		    	}
		    	var status = $(e.currentTarget).attr("data-status");
		    	var roomPrice = $(e.currentTarget).find("ul li:first-child").find("font").text();
		    	var confirmTip = null;
		    	if(status == "Dirty"){
		    		confirmTip = "此房为脏房状态，是否继续办理？";
		    	}
		    	if(status == "Repairing"){
		    		confirmTip = "此房为维修房状态，是否继续办理？";
		    	}
		    	if(roomPrice == " ￥ 未定义 "){
		    		confirmTip = "此房房型未定价，请定价后再选择该房入住！";
		    		Dialog.alert({
		    			title:"提示",
		    			content: confirmTip,
		    		});
		    		return false;
		    	}
		    	
		    	if(confirmTip){
		    		Dialog.confirm({
		    			title:"提示",
		    			content: confirmTip,
		    			confirm:function(){
		    				Dialog.close();
		    				setTimeout(function(){
		    					if(that.get("model").flag == "ExchangeRoom"){
		    			    		//选中该房间，并返回界面
		    		    			that.selectRoom(e);
		    			    	}else{
		    			    		that.selectPersonInfo(e);
		    			    	}
		    				},300);
		    			}
		    		});
		    	}else{
		    		if(that.get("model").flag == "ExchangeRoom"){
			    		//选中该房间，并返回界面
		    			that.selectRoom(e);
			    	}else{
			    		that.selectPersonInfo(e);
			    	}
		    	}
		    }
    	},
    	selectPersonInfo:function(e){
    		var that = this;
    		var pkRoom = $(e.currentTarget).attr("data-pkRoom");
	    	var roomRegister = that.get("roomRegister")?that.get("roomRegister"):{};
	    	var personInfoList = [];
	    	var list = that.get("editgrid").getData();
	    	
	    	var form = null;
	    	if(that.get("showForm")=='.J-form'){
    			form = that.get("form");
    		}else{
    			form =that.get("doorForm");
    		}
	    	
	    	var pkCIImplement = form.getValue("pkCIImplement");
        	var sourceType = form.getValue("sourceType");
        	var signUpType = form.getValue("signUpType");
        	var isVerify = false;
        	if(pkCIImplement){
        		if(signUpType == "NonMember" && sourceType == "DropIn"){
        			isVerify = false;
        		}else{
        			isVerify = false;	
        			if(signUpType == "Experience"){
        				isVerify = true;
        			}
        		}
        	}else{
        		isVerify = false;
        	}
	    	
	    	
	    	for(var i=0;i<list.length;i++){
	    		personInfoList.push(list[i].nonMemberRegister.signUpRegister.personalInfo);
	    	}
	    	var roomPanoramaDialog = new RoomPanoramaDialog({
	    		widget:that,
	    		model:{
	    			personInfoList:personInfoList,
	    			isVerify: isVerify,
	    			data:roomRegister[pkRoom]?roomRegister[pkRoom]["registerPersonInfos"]:[]
	    		}
	    	});
	    	Dialog.showComponent(roomPanoramaDialog,{
                title : "入住人员登记",
                setStyle : function(){
                	$(".modal").width(500);
                	$(".modal").height(361);
                	$(".modal .J-dialog-conponent").height(230);
                	$(".modal .J-dialog-text").hide();
                },
                confirm : function(){
                	var floorData = that.get("roomData");
                	var roomData = [];
                	for(var i=0;i<floorData.length;i++){
                		roomData = roomData.concat(floorData[i].roomlist)
                	}
                	
                	var roomObj = $.grep(roomData,function(n,i){
                		if(n.pkRoom == pkRoom){
                			return n;
                		}
                	})[0];
                	
                	var roomTypeData = that.get("subnav").getData("roomType");
                	var pkOrgBuilding = that.get("subnav").getValue("building");
                	for(var i=0;i<roomTypeData.length;i++){
                		if(roomTypeData[i].pkRoomType == roomObj.pkRoomType){
                			roomObj.roomType = roomTypeData[i];
                		}
                	}
                	
                	roomObj.pkOrgBuilding = pkOrgBuilding;
                	
                	if(!roomRegister[pkRoom]){
                		roomRegister[pkRoom]={};
                	}
                	roomRegister[pkRoom]["room"] = roomObj;
                	roomRegister[pkRoom]["registerPersonInfos"] = roomPanoramaDialog.getData(); 
                	that.set("roomRegister",roomRegister);
                	that.setRoomPersonInfo();
                }
            });
    	},
    	setRoomPersonInfo:function(){
    		var roomRegister = this.get("roomRegister");
    		if(roomRegister){
    			var roomdata = this.get("roomData");
    			var html = HandlerBars.compile(roomTpl)(roomdata);
            	$(".J-room",this.element).html(html);
            	$(".el-room-check",this.element).show();
				$(".room-piece").addClass("room-select");
				
    			for(var key in roomRegister){
    				
    				if(roomRegister[key]["registerPersonInfos"].length > 0){
    					$("#room-piece-"+key,this.element).find(".el-room-check").addClass("active");
    					
    					var arrHtml = [];
    					for(var i =0;i<roomRegister[key]["registerPersonInfos"].length;i++){
    						arrHtml.push("<li>"+roomRegister[key]["registerPersonInfos"][i].name+"</li>");
    					}
    					
    					$("#room-piece-"+key,this.element).find(".el-room-price ul").html(arrHtml.join(""));
    					
    				}else{
    					$("#room-piece-"+key,this.element).find(".el-room-check").removeClass("active");
    				}
    			}
    			
    		}
    		
    	}
    };
});
